#include "PlayOffBehaviour.h"

// Rozhodovanie sa mimo modu play_on (cas je zastaveny).
// POZN.: Vyuzit zastavenie casu na pozorovanie okolia! (napr. 1x sa otocit okolo vlastnej osi)
void PlayOffBehaviour::Behave()
{
#ifndef GOALIE
	PlayerRunPlayOff();
#endif

	PlayMode pmode = world->state->GetPlayMode();
	if(pmode == PM_BeforeKickOff || pmode == PM_AfterGoal_Our || pmode == PM_AfterGoal_Their)
	{
		Point home = DATA_STORAGE.GetHomePos();
		
		if((home - world->me->pos).GetLength() > POS_TOL)
		{
			GetMoveBehaviour()->Behave(home);
		}
		else
		{
			GetTurnToBehaviour()->Behave(Point(0, 0));
		}
		
		GetTurnNeckToBehaviour()->NeckSynchronizationWithVI(ALIG_NECK);
    }
	else
	{
		// Pravdepodobne som v ofsajde
#ifndef GOALIE
		
		const float off_l = DATA_STORAGE.GetOffsideLine();
		
		if(world->me->pos.GetX() > off_l - 0.1f)
		{
			world->me->pos.SetX(off_l - 0.1f);
		}

		// Odbehni z offsajdu
		GetRunToPositionBehaviour()->Behave(world->me->pos, 1);
		GetTurnNeckToBehaviour()->NeckSynchronizationWithVI(ALIG_NECK);
#endif
	}
}

//spravanie hraca v poli v pocas modu play off, suvisi s nastavenim veduceho hry atd
void PlayOffBehaviour::PlayerRunPlayOff()
{
	//printf("\nActBehaviour::PlayerRunPlayOff()");
	//ak este nik nebol veduci a som hrac cislo 5, nastavim sa ako veduci
	if ((!DATA_STORAGE.leadership.amILeader && DATA_STORAGE.leadership.leaderUniform == -1) && (world->me->GetUniform() == 5))
		DATA_STORAGE.leadership.amILeader = true;

	// Ak nikoho nepocuvam a nie som veduci a viem kto je veduci, tak si nastavim nanho attentionto
	if (!DATA_STORAGE.leadership.attentionToSet && !DATA_STORAGE.leadership.amILeader)
	{
		if (DATA_STORAGE.leadership.leaderUniform > 0)
		{
			commandGenerator->AttentionTo(DATA_STORAGE.leadership.leaderUniform);
			DATA_STORAGE.leadership.attentionToSet = true;
		}
	}

	//ak som veduci a zapas este nezacal, oznamujem to ostatnym
	if (DATA_STORAGE.leadership.amILeader && DATA_STORAGE.leadership.lastChangeTime == -1 && world->state->GetSimTime() == 0)
	{
		DATA_STORAGE.leadership.casZmenyVeduceho = 0;
		GetLeadershipBehaviour()->SendLeaderMSG();
		DATA_STORAGE.leadership.lastChangeTime = 0;
	}
}
